;(function ($, player) {

    function MusicPLayer(dom) {
        this.wrap = dom // 播放器容器（用于加载ListControl模块）
        this.now = 1
        this.dataList = []
        this.rotateTimer = null
        this.curIndex = 0
        this.list = null
        this.propress = player.progress.pro()
    }

    MusicPLayer.prototype = {
        init(){
            this.getDom() // 获取元素
            this.getData('../mock/data.json') // 请求数据
           
        },
        getDom(){ // 获取页面里的元素
            this.record = document.querySelector('.songImg img') // 旋转图片
            this.controlBtns = document.querySelectorAll('.control li') // 底部导航栏按钮
        },
        getData(url){
            const _this = this
            $.ajax({
                url,
                method:'get',
                success(res){
                    console.log('dataList',res)
                    _this.dataList = res
                    _this.listPlay()
                    _this.loadMusic(_this.now) 
                    _this.musicControl()
                    _this.indexObj = new player.controlIndex(res.length)
                    _this.dragProperss()
                },
                err(e){
                    console.log('err',e)
                }
            })
        },
        loadMusic(index){
            const _this = this
            const curr = this.dataList[index]
            player.render(curr) // 渲染图文
            // player.music.end(function () {
            //     _this.propress.renderAllTime(curr.duration)
            // })
            player.music.load(curr.audioSrc) // 播放音乐
            this.propress.renderAllTime(curr.duration)

            // 播放
            if(player.music.status == 'play'){
                player.music.play()
                this.controlBtns[2].className = 'playing'
                this.imgRotate(0)
                this.propress.stop() // 清空上一次的进度
                this.propress.move(0)
            }

            this.list.changeSelect(index)
            this.curIndex = index
            player.music.end(function () {
                player.music.status = 'play'
                _this.loadMusic(_this.indexObj.next())
            })
        },
        musicControl(){ // 控制音乐 上一首 暂停
            player.music 
            const _this = this
            // 上一首
            this.controlBtns[1].addEventListener('touchend', function () {
                player.music.status = 'play'
                _this.loadMusic(_this.indexObj.prev())
            })
            // 播放 暂停
            this.controlBtns[2].addEventListener('touchend', function () {
                if(player.music.status == 'play'){ // 暂停
                    player.music.pause()
                    _this.controlBtns[2].className = ''
                    _this.imgStop()
                    _this.propress.stop()
                }else{
                    player.music.play()
                    _this.controlBtns[2].className = 'playing'
                    const deg = _this.record.dataset.rotate || 0;
                    _this.imgRotate(deg)
                    _this.propress.move()
                }
               
            })
            // 下一首
            this.controlBtns[3].addEventListener('touchend', function () {
                player.music.status = 'play'
                _this.loadMusic(_this.indexObj.next())
            })
        },
        imgRotate(deg){
            this.imgStop()
            this.rotateTimer = setInterval(()=>{
                deg = +deg + 0.2

                this.record.style.transform = `rotate(${deg}deg)`
                this.record.dataset.rotate = deg
            }, 1000/60)
        },
        imgStop(){
            clearInterval(this.rotateTimer)
        },
        listPlay(){ // 列表切歌
            this.list = player.listControl(this.dataList, this.wrap)
            const _this = this
            // 列表按钮
            this.controlBtns[4].addEventListener('touchend', function (params) {
                _this.list.sideUp()
            })
            //歌曲列表
            this.list.musicList.forEach((item, index) => {
                item.addEventListener('touchend',function () {
                    if(this.curIndex == index) return;
                    _this.indexObj.index = index
                    player.music.status = 'play'
                    _this.loadMusic(index)
                    _this.list.sideDown()
                })
            });
        },
        dragProperss(){
            const _this = this;
            
            const drag = player.progress.drag(document.querySelector('.circle')); // 添加进度条功能
            drag.init()
            drag.start = function () {
                _this.propress.stop()
            }
            drag.move = function (per) {
                _this.propress.update(per) // 更新进度条背景
            }
            drag.end = function (per) {
                const cutTime = per * _this.dataList[_this.indexObj.index].duration;
                console.log('cutTime',cutTime)
                player.music.playTo(cutTime) // 更新歌曲进度
                player.music.play()
                _this.controlBtns[2].className = 'playing'
                _this.propress.move(per)
                const deg = _this.record.dataset.rotate || 0;
                _this.imgRotate(deg)
                // 拖拽到最后 cutTime
                if(cutTime === _this.dataList[_this.indexObj.index].duration){
                   
                }
            }
        }
    }

    const musicPlayer = new MusicPLayer(document.getElementById('wrop'))
    musicPlayer.init()
    
})(window.Zepto, window.player);